import store from '@/store/index.js'
import {BASEURL} from '@/utils/request.js'

export default {
	isShowRow(){
		
	},
	xcxImgUrl(){
		return 'https://erp.zhuiyun.net/mall/file/'
	},
	/* 获取元素距离顶部的高度
	cla : 节点类名
	returnValue:是否需要返回值
	*/
	getDemoTopHeight(cla) {
		setTimeout(()=>{
			let info = uni.createSelectorQuery().select("." + cla);
			info.boundingClientRect((data) => { //data - 各种参数
					store.commit('updateSearchTop', data.top)
					console.log(data, 'vuex') // 获取元素宽度
			}).exec()
		},500)
	},
	queryDom(dom,that){
		return new Promise((resolve) => {
			let query = uni.createSelectorQuery().in(that);
			query.select("#"+dom).boundingClientRect(r => {
				console.log(r)
				resolve(r.height);
			}).exec();
		})
	},
	/* 计算时间 返回相差天 时 分 秒 */
	getTime(time,type) {
		var nowtime = new Date(), //获取当前时间
			endtime = new Date(time); //定义结束时间
		var lefttime = endtime.getTime() - nowtime.getTime(), //距离结束时间的毫秒数
			leftd = Math.floor(lefttime / (1000 * 60 * 60 * 24)), //计算天数
			lefth = Math.floor(lefttime / (1000 * 60 * 60) % 24), //计算小时数
			leftm = Math.floor(lefttime / (1000 * 60) % 60), //计算分钟数
			lefts = Math.floor(lefttime / 1000 % 60); //计算秒数
		// return leftd + "天" + lefth + ":" + leftm + ":" + lefts; //返回倒计时的字符串
		if(type == 'day'){
			return leftd
		}else if(type == 'miao'){
			return lefttime
		}
	},
	/**
	 * 提示信息
	 * @param {Object} title 提示文字
	 * @param {Object} icon 图标 success 成功 error 失败 fail 错误  exception 异常
	 */
	toast(title,icon='none'){
		uni.showToast({
			title,
			icon,
			duration:3000
		})
	},
	/**
	 * 获取拍照
	 * @param {Object} sourceType //album 从相册选图，camera 使用相机
	 */
	takeAPhoto(sourceType){
		return new Promise(resolve=>{
			uni.chooseImage({
				count: 6, //默认9
				sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				sourceType:sourceType,   //album 从相册选图，camera 使用相机
				success: function (res) {
					console.log(JSON.stringify(res)); //拍照图片的路径
					resolve(res) 
				}
			});
		})
	},
	/**
	 * 上传图片
	 * @param {Object} url 
	 */
	uploadFilePromise(url) {
		return new Promise((resolve, reject) => {
			console.log(url,BASEURL,666);
			uni.showLoading()
			let a = uni.uploadFile({
				url:BASEURL + 'sys/mall/batchUploadFile', // 仅为示例，非真实的接口地址
				filePath: url,
				name: 'file',
				header:{
					accessToken:uni.getStorageSync('visitorAccessToken') ? JSON.parse(uni.getStorageSync('visitorAccessToken')) : ''
				},
				formData: {
					group:'06'
				},
				success: (res) => {
					uni.hideLoading()
					setTimeout(() => {
						resolve(JSON.parse(res.data))
					}, 1000)
				},
				
			});
		})
	},
	/**
	 * 小程序获取手机号
	 */
	getPhoneNumber(){
		
	},
	
	/**
	 * 时间戳转日期格式
	 * @param {String} arr 时间戳
	 *  @param {string} type type 不传 拼接到时分秒 'nyr' 年月日
	 */
	DateFormateTime(arr,type=''){
		if(arr == '' || arr == null || arr == undefined){
			return '-'
		}
		 // 时间戳为10位需*1000，时间戳为13位不需乘1000
		  var date = new Date(arr);
		  var Y = date.getFullYear() + "-";
		  var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
		  var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
		  var h = date.getHours() + ":";
		  var m = date.getMinutes() + ":";
		  var s = date.getSeconds();
		  if(type == 'nyr'){
			return Y + M + D
		  }else{
			return Y + M + D + h + m + s;  
		  }
		  
	},
	
	/**
	 * 时间数组处理格式
	 * arr 日期数组
	 * type 不传 拼接到时分秒 'nyr' 年月日
	 */
	DateFormateArr(arr, type = ""){
		if (!arr || !arr.length) {
			return '-'
		}
		let year = ''
		let time = new Date(...arr).getTime()
		if (time) {
			let seconds = time;
			let onetime = new Date(); //声明一个日期内建函数
			onetime.setTime(seconds);
			let a = onetime.getFullYear(); //得到年份
			let b = (onetime.getMonth()).toString().padStart(2, '0'); //得到月份
			let c = onetime.getDate().toString().padStart(2, '0'); //得到日期
			let s = onetime.getHours().toString().padStart(2, '0');
			let f = onetime.getMinutes().toString().padStart(2, '0');
			let m = onetime.getSeconds().toString().padStart(2, '0');
			if (type == 'nyr') {
				year = a + '-' + b + '-' + c
			} else {
				year = a + '-' + b + '-' + c + ' ' + s + ':' + f + ':' + m
			}
	
		}
		return year
	
	},
	/**
	 *计算当前时间加上几天后的时间 
	 */
	// date为相加前的时间， days 为 需要相加的天数
	addDate(days) {
	  var date = new Date()
	  date.setDate(date.getDate() + days);
	  var year = date.getFullYear();
	  var month = date.getMonth() + 1;
	  var day = date.getDate();
	  var mm = "'" + month + "'";
	  var dd = "'" + day + "'";
	  if(mm.length == 3) {
	    month = "0" + month;
	  }
	  if(dd.length == 3) {
	    day = "0" + day;
	  }
	  var time = year + "-" + month + "-" + day
	  return time;
	},
	/**
	 * 拨打电话
	 */
	call(phone){
			const res = uni.getSystemInfoSync();
			// ios系统默认有个模态框
			if (res.platform == 'ios') {
				uni.makePhoneCall({
					phoneNumber: phone,
					success() {
						console.log('拨打成功了');
					},
					fail() {
						console.log('拨打失败了');
					}
				})
			} else {
				//安卓手机手动设置一个showActionSheet
				uni.showActionSheet({
					itemList: [phone, '呼叫'],
					success: function(res) {
						console.log(res);
						if (res.tapIndex == 1) {
							uni.makePhoneCall({
								phoneNumber: phone,
							})
						}
					}
				})
			}
	
	
	},
	/**
	 * 保留小数位树
	 * @param {Object} value 数量 金额 单价 
	 * @param {Object} type number price money
	 */
	formatToFixed(value,type){
		if(value == 0){
			return value
		}
		if(type == 'number'){
			if(value % 1 == 0){
				return (value*1).toFixed(0);
			}else{
				// return value.toFixed(2);
			return ((value*1).toFixed(store.state.configSetting.numberDecimal)*1).toFixed(2)*1;
			}
		}
		if(type == 'price'){
			if(value % 1 == 0){
				return (value*1).toFixed(0);
			}else{
				// return value.toFixed(2);
			return ((value*1).toFixed(store.state.configSetting.priceDecimal)*1).toFixed(2)*1;
			}
		}
		if(type == 'money'){
			if(value % 1 == 0){
				return (value*1).toFixed(0);
			}else{
				// return value.toFixed(2);
		return ((value*1).toFixed(store.state.configSetting.moneyDecimal)*1).toFixed(2)*1;
 		}
		}
	},
	/**
	 * 单据类型
	 * @param {Object} type 单据类型
	 */
	getBillTypeName (type) {
	    var typeName = "";
	    switch (type) {
	      case '101': typeName = "采购订单"; break;
	      case '102': typeName = "采购入库单"; break;
	      case '103': typeName = "采购退货单"; break;
	      case '104': typeName = "采购换货单"; break;
	      case '105': typeName = "获赠单"; break;
	
	      case '201': typeName = "销售订单"; break;
	      case '202': typeName = "销售出库单"; break;
	      case '203': typeName = "销售退货单"; break;
	      case '204': typeName = "销售换货单"; break;
	      case '205': typeName = "零售单"; break;
	      case '206': typeName = "赠送单"; break;
	
	      case '301': typeName = "发货单"; break;
	      case '302': typeName = "收货单"; break;
	      case '303': typeName = "盘点单"; break;
	      case '304': typeName = "报损单"; break;
	      case '305': typeName = "报溢单"; break;
	      case '306': typeName = "调拨单"; break;
	      case '307': typeName = "组装拆分单"; break;
	      case '308': typeName = "其它出库单"; break;
	      case '309': typeName = "其它入库单"; break;
	      case '311': typeName = "调拨申请单"; break;
	      case '312': typeName = "调拨验收单"; break;
	      case '313': typeName = "调拨退回单"; break;
	      case '314': typeName = "组装拆分模板"; break;
	      case '315': typeName = "拆分单"; break;
		  case 'allotReturn' : typeName = '调拨验收退回';break;
	      case '401': typeName = "外部费用单"; break;
	      case '402': typeName = "内部费用单"; break;
	      case '404': typeName = "成本调价单"; break;
	      case '405': typeName = "采购费用分摊单"; break;
	      case '406': typeName = "业务凭证"; break;
	      case '407': typeName = "转款单"; break;
	      case '408': typeName = "销售费用分摊单"; break;
	
	      case '501': typeName = "往来核销单"; break;
	      case '502': typeName = "收款单"; break;
	      case '503': typeName = "付款单"; break;
	      case '504': typeName = "预收款单"; break;
	      case '505': typeName = "预付款单"; break;
	      case '506': typeName = "应收增加单"; break;
	      case '507': typeName = "应收减少单"; break;
	      case '508': typeName = "应付增加单"; break;
	      case '509': typeName = "应付减少单"; break;
				
		case '601': typeName = "报单"; break;
		case '607': typeName = "返利单"; break;
		case '608': typeName = "赠品单"; break;
	    
	    }
	    return typeName;
	  },
}
